//
//  GRCurrentCityHeader.m
//  YC_GRice
//
//  Created by Feng Liu on 2018/8/27.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRCurrentCityHeader.h"

@interface GRCurrentCityHeader ()

@property (nonatomic, strong) UIView *locationView;
@property (nonatomic, strong) UIView *cityView;

@end


@implementation GRCurrentCityHeader

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self drawUI];
    }
    return self;
}

- (void)drawUI {
    self.backgroundColor = [UIColor whiteColor];
    UILabel *line            = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, KscreenWidth, 0.7)];
    line.backgroundColor = KGMBACKGROUND_COLOR;
    [self addSubview:line];
    
    self.locationView      = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:self.locationView];
    
    UILabel *locationLabel  = [[UILabel alloc] init];
    locationLabel.text        = @"定位到当前城市";
    locationLabel.font        = [UIFont boldSystemFontOfSize:15];
    locationLabel.textColor = KGMBLACK_COLOR;
    [self.locationView addSubview:locationLabel];
    WEAKSELF
    [locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakSelf.locationView.mas_centerX).offset(7.5);
        make.centerY.mas_equalTo(weakSelf.locationView.mas_centerY);
    }];
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Home_09"]];
    [self.locationView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(locationLabel.mas_left).offset(-5);
        make.centerY.mas_equalTo(locationLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    
    _cityView     = [[UIView alloc] initWithFrame:self.bounds];
    [self addSubview:self.cityView];
    
    _currentCity = [[UILabel alloc] init];
    _currentCity.font = [UIFont boldSystemFontOfSize:15];
    _currentCity.textColor = KGMBLACK_COLOR;
    [self.cityView addSubview:_currentCity];

    [_currentCity mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.cityView.mas_left).offset(15);
        make.centerY.mas_equalTo(weakSelf.cityView.mas_centerY);
    }];
    
    UILabel *gpsLabel  = [[UILabel alloc] init];
    gpsLabel.text        = @"当前城市";
    gpsLabel.font        = [UIFont systemFontOfSize:12];
    gpsLabel.textColor = HEXCOLOR(0x999999);
    [self.cityView addSubview:gpsLabel];
    [gpsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.currentCity.mas_right).offset(10);
        make.centerY.mas_equalTo(weakSelf.currentCity.mas_centerY);
    }];
    self.cityView.hidden = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationCurrentCity)];
    [self.locationView addGestureRecognizer:tap];
}

-(void)setIsLocation:(BOOL)isLocation {
    self.cityView.hidden      = isLocation;
    self.locationView.hidden = !isLocation;
}

- (void)locationCurrentCity {
    if (self.delegate && [self.delegate respondsToSelector:@selector(backUpViewController)]) {
        [self.delegate backUpViewController];
    }
    
    GRLocationManager *locationManager = [GRLocationManager defaultManager];
    locationManager.city          = locationManager.currentpoi.city;
    locationManager.adcode      = locationManager.currentpoi.adcode;
    locationManager.coordinate = CLLocationCoordinate2DMake(locationManager.currentpoi.location.latitude, locationManager.currentpoi.location.longitude);

    [[NSNotificationCenter defaultCenter] postNotificationName:GRDidFinishLocationNotification object:self userInfo:@{LocationKey : [GRLocationManager defaultManager].currentpoi, DragMapKey : @(NO),FreshLoc:@(YES)}];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
